package it.fuesi.expired.model;

import it.fuesi.expired.model.base.AModel;
import it.fuesi.expired.model.collections.TagCollection;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Item extends AModel{

	private String name;
	private Date expireDate;
	private TagCollection tags;
	
	public Item(){
		super();
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getExpireDate() {
		return expireDate;
	}
	public String getExpireDate(String dateFormat){
		final SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		return df.format(expireDate);
	}
	
	public void setExpireDate(Date expireDate) {
		this.expireDate = expireDate;
	}
	public void setExpireDate(String date, String dateFormat){
		final SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		try {
			setExpireDate(df.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public TagCollection getTags() {
		return tags;
	}
	public String getTagsToString(String separator){
		String tags = "";
		if(this.tags != null && this.tags.getCount() > 0){
			for(Tag t : this.tags.getList())
				tags += t.getName() + separator;
			tags.substring(0,tags.length()-separator.length());
		}
		return tags;
	}
	public void setTags(TagCollection tags) {
		this.tags = tags;
	}
	
	public void addTag(Tag tag){
		if(tags == null)
			tags = new TagCollection();
		
		tags.add(tag);
	}
	
	public void addTag(String tag){
		if(tag != null && !tag.trim().isEmpty())
			addTag(new Tag(tag));
	}
	
	@Override
	public String toString() {
		return name;
	}

	
}
